import Vue from "vue";
import Vuex from "vuex";

Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    adminname: sessionStorage.getItem("adminname") || "",
    admintoken: sessionStorage.getItem("admintoken") || "",
    loginUser: localStorage.getItem("loginUser") || "",
    identity: localStorage.getItem("identity") || "",
    token: localStorage.getItem("mcyztoken") || "",
  },
  getters: {},
  mutations: {
    // 存管理员信息
    updateAdminInfo(state, payload) {
      // 将用户名存入state
      state.adminname = payload;
      sessionStorage.setItem("adminname", payload);
    },
    setAdminToken(state, payload) {
      state.admintoken = payload;
      sessionStorage.setItem("admintoken", payload);
    },
    // 管理员退出
    logout(state) {
      state.adminname = ""; // 将vuex中的数据清空
      state.admintoken = "";
      sessionStorage.removeItem("adminname"); // sessionStorage
      sessionStorage.removeItem("admintoken");
    },
    // 存用户昵称
    setLoginUser(state, nickname) {
      state.loginUser = nickname;
    },
    // 存用户身份
    setIdentity(state, identity) {
      state.identity = identity;
    },
    setToken(state, token) {
      state.token = token;
    },
    // 用户退出登录
    quitLogin(state) {
      state.loginUser = "";
      state.identity = "";
      state.token = "";
      localStorage.removeItem("loginUser");
      localStorage.removeItem("identity");
      localStorage.removeItem("mcyztoken");
    },
  },
  actions: {},
  modules: {},
});
